﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace Camping_Project.Forms
{
    public partial class FormModifierClient : Form
    {

        private int nb = 0;
        FormEmploye emp;

        public FormModifierClient(FormEmploye emp)
        {
            InitializeComponent();
            this.emp = emp;
        }


        public void refreshDGVClient()
        {
            //Rafraichissement de la DGV client
            Camping_BDD bdd = new Camping_BDD();
            this.dataGridView1.DataSource = from c in bdd.Client select new { c.IDClient, c.Nom, c.Prenom, c.DateNaissance, c.Adresse, c.CodePostal, c.Ville, c.Pays, c.Telephone, c.Email };
        }

        public void refreshDGVReserv()
        {

            //Rafraichissement de la DGV reservation liée au client sélectionné
            Camping_BDD bdd = new Camping_BDD();
            int ligne = dataGridView1.SelectedCells[0].RowIndex;
            int id = (int)dataGridView1["iDClientDataGridViewTextBoxColumn", ligne].Value;

            this.dataGridView2.DataSource = from reservation in bdd.Reservation
                                            where reservation.IDClient == id
                                            select new
                                            {
                                                reservation.IDReservation,
                                                reservation.DateDebut,
                                                reservation.DateFinApprox,
                                                reservation.DateFinDef,
                                                reservation.TypeEmp,
                                                reservation.NbAdultes,
                                                reservation.NbEnfants,
                                                reservation.NbAnimaux,
                                                reservation.NbVehicules,
                                                reservation.Electricite,
                                                reservation.PrixNuit,
                                                reservation.PrixAdulte,
                                                reservation.PrixEnfant,
                                                reservation.PrixAnimal,
                                                reservation.PrixElectricite,
                                                reservation.PrixTotal,
                                                reservation.PayementEffectue,
                                                reservation.IDClient,
                                                reservation.IDEmplacement,
                                                reservation.IDEmploye,
                                                reservation.Valide
                                            };

        }

        private void FormModifierClient_Load(object sender, EventArgs e)
        {
            refreshDGVClient();
            dataGridView1.Columns[".."].ReadOnly = false;
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            refreshDGVReserv();
        }

        public void clearDGV()
        {
            dataGridView2.Rows.Clear();
        }


        private void delClient_Click(object sender, EventArgs e)
        {

            Camping_BDD bdd = new Camping_BDD();
            int ligne = dataGridView1.SelectedCells[0].RowIndex;
            int id = (int)dataGridView1["iDClientDataGridViewTextBoxColumn", ligne].Value;
            Client clientASupprimer = (from c in bdd.Client where c.IDClient == id select c).First();
            // Reservation reservationASupprimer = (from r in bdd.Reservation where r.IDClient == id select r).First();
            DialogResult dlgResult = MessageBox.Show("Etes vous sur de supprimer le client et toutes ses réservations? " + clientASupprimer.Nom.Trim() + " " + clientASupprimer.Prenom.Trim() + " ?", "Continuer?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            
            //Selection de tous les éléments réservations liés au client

            var queryReservation =
                                    from reservation in bdd.Reservation
                                    where reservation.IDClient == id
                                    select reservation;

            if (dlgResult == DialogResult.Yes)
            {
                //Suppression de chaque réservation liée au client
                foreach (var del in queryReservation)
                {
                    bdd.Reservation.DeleteObject(del);
                }


                bdd.Client.DeleteObject(clientASupprimer);

                try
                {
                    bdd.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    // Provide for exceptions.
                }
                refreshDGVClient();
                //clearDGV();

            }

            else if (dlgResult == DialogResult.No)
            {
                // Ben pas de suppression tiens ! :p
            }


        }

        private void btnModifierClient_Click(object sender, EventArgs e)
        {
            Camping_BDD bdd = new Camping_BDD();
            int ligne = dataGridView1.SelectedCells[0].RowIndex;
            int id = (int)dataGridView1["iDClientDataGridViewTextBoxColumn", ligne].Value;
            Client clientAModifier = (from c in bdd.Client where c.IDClient == id select c).First();

            FormModifInfosClient fmi = new FormModifInfosClient(clientAModifier);
            fmi.MdiParent = emp;
            fmi.PerformLayout();
            fmi.Show();

        }


    }


}
